package org.arkeologgr.appening.service.json;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.arkeologgr.appening.entity.Item;
import org.arkeologgr.appening.entity.ItemComment;
import org.arkeologgr.appening.entity.ItemDetails;
import org.arkeologgr.appening.entity.ItemImage;
import org.arkeologgr.appening.entity.ServiceUser;
import org.arkeologgr.appening.entity.Item.ItemSource;
import org.arkeologgr.appening.service.InternalAdapter;


public class JSONConverter {

	public static List<ItemCommentJSON> toJsonComment(List<ItemComment> icList) {
		
		List<ItemCommentJSON> jsonList = new ArrayList<ItemCommentJSON>();
		for (ItemComment ic : icList) {
			jsonList.add(JSONConverter.toJsonComment(ic));
		}
		return jsonList;
	}

	public static ItemCommentJSON toJsonComment(ItemComment ic) {
		return new ItemCommentJSON(ic);
	}
	
	public static Item toEntity(ItemJSON jsonItem) {
		return null;
	}

	public static ItemDetailsJSON toJsonItemDetail(ItemDetails itemDetails) {
		return new ItemDetailsJSON(itemDetails);
	}

	public static List<ItemJSON> toJsonItem(List<Item> itemList, List<ItemDetails> ksamItems) {
		List<ItemJSON> jsonList = new ArrayList<ItemJSON>();
		for (Item it : itemList) {
			List<ItemComment> comments = InternalAdapter.lookup().findComments(it);
			ItemJSON jsonItem = JSONConverter.toJsonItem(it);
			jsonItem.setCommentCount(comments.size());
			jsonList.add(JSONConverter.toJsonItem(it));
		}
		for (ItemDetails itemDetails : ksamItems) {
			List<ItemComment> comments = InternalAdapter.lookup().findComments(itemDetails.getItem());
			ItemJSON jsonItem = JSONConverter.toJsonItem(itemDetails.getItem());
			String title = jsonItem.getTitle() + itemDetails.getDescription();
			jsonItem.setTitle(StringUtils.abbreviate(title, 64));
			jsonItem.setCommentCount(comments.size());
			jsonList.add(JSONConverter.toJsonItem(itemDetails.getItem()));
			
		}
		
		
		return jsonList;
	}

	public static ItemJSON toJsonItem(Item it) {
		return new ItemJSON(it);
	}

	public static ItemImage toEntity(AddItemJSON addItem, Item item) {
		ItemImage itemImage = new ItemImage();		
		itemImage.setRawImage(addItem.getImage());
		itemImage.setEntityUri(item.getEntityUri());
		return itemImage;
	}
	
	public static ItemDetails toEntity(AddItemJSON addItem) {
		Item item = new Item();
		ItemDetails itemDetails = new ItemDetails();
		ServiceUser user = InternalAdapter.lookup().getUser(addItem.getUserName());
		
		item.setCreator(user);
		// item.setEntityUri("http://entity.capgemini.com/internal/test1234");
		String newUri = "http://entity.capgemini.com/internal/" + UUID.randomUUID();
		item.setEntityUri(newUri);
		item.setItemSource(ItemSource.Internal);
		item.setLatitude(addItem.getLatitude());
		item.setLongitude(addItem.getLongitude());
		item.setThumbUrl("");
		item.setTitle(addItem.getTitle());
		
		itemDetails.setDescription(addItem.getDescription());
		itemDetails.setImageUrl("");
		itemDetails.setItem(item);
		
		return itemDetails;
	}

	public static ItemComment toEntity(AddCommentJSON addComment) {
		InternalAdapter adapter = InternalAdapter.lookup();
		ServiceUser user = adapter.getUser(addComment.getUserName());
		ItemComment ic = new ItemComment(addComment.getEntityUri(),user);
		ic.setComment(addComment.getComment());
		ic.setTimestamp(new Date());
		return ic;
	}
	
	
}
